package com.aibili.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aibili.pojo.FeedBack;
import com.aibili.pojo.GiftInfo;
import com.aibili.pojo.HelpInfo;
import com.aibili.pojo.HobbyInfo;
import com.aibili.pojo.LvInfo;
import com.aibili.pojo.MomentInfo;
import com.aibili.pojo.ReportInfo;
import com.aibili.pojo.UserAccount;
import com.aibili.pojo.UserAnonymous;
import com.aibili.pojo.UserFavorability;
import com.aibili.pojo.UserInfo;
import com.aibili.pojo.UserLocation;
import com.aibili.pojo.VcodeRecord;
import com.aibili.pojo.VideoEvaluate;
import com.aibili.pojo.model.PageBodyModel;
import com.aibili.pojo.model.PageModel;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.DsConstant;

@Service("userInfoService")
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class UserInfoService extends BaseService {
	/**
	 * 登录查询用户信息
	 * 
	 * @param mobile
	 * @return
	 */
	public UserInfo login(String mobile) {
		String hql = " from UserInfo where mobile = :mobile";
		UserInfo info = (UserInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "mobile" }, mobile);
		return info;
	}

	/**
	 * 验证码五分钟有效
	 * 
	 * @param phone
	 * @param vcode
	 * @return
	 */
	public long getPhoneCode(String phone, String vcode) {
		String hql = "select count(id) from VcodeRecord where vcode = :vcode and phone = :phone and timestampdiff(minute ,time,now()) < 5";
		long count = (Long) dataDao.getFirstObjectViaParam(hql, new String[] { "vcode", "phone" }, vcode, phone);
		return count;
	}

	/**
	 * 注册添加用户信息
	 * 
	 * @param mobile
	 * @param password
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public UserInfo addUserInfo(String mobile, String password, String gender, String token) {
		UserInfo info = new UserInfo();
		info.setMobile(mobile);
		info.setUser_name(mobile);
		info.setPassword(password);
		info.setGender(gender);
		info.setBirthday("1990-01-01");
		info.setYx_reg(token);
		info.setLevel(1);
		info.setExp(0);
		info.setIs_seale(0);
		info.setIs_home(0);
		dataDao.addObject(info);
		return info;
	}

	/**
	 * 验证码
	 * 
	 * @param phone
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public String getVcode(String phone) {
		String vcode = "";
		// 获取6位随机数
		vcode = String.valueOf(new Random().nextInt(900000) + 100000);
		String hql = "from VcodeRecord where phone = :phone";
		VcodeRecord info = (VcodeRecord) dataDao.getFirstObjectViaParam(hql, new String[] { "phone" }, phone);
		if (info != null) {
			info.setVcode(vcode);
			info.setTime(new Date());
			dataDao.updateObject(info);
		} else {
			info = new VcodeRecord();
			info.setVcode(vcode);
			info.setTime(new Date());
			info.setPhone(phone);
			dataDao.addObject(info);
		}
		return vcode;
	}

	/**
	 * 修改用户信息
	 * 
	 * @param id
	 * @param mobile
	 * @param password
	 * @param username
	 * @param portrait
	 * @param level
	 * @param exp
	 * @param point
	 * @param description
	 * @param idcard
	 * @param birthday
	 * @param age
	 * @param income
	 * @param height
	 * @param location
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public UserInfo updateUserInfo(String user_id, String mobile, String username, String portrait, String level,
			String idcard, String birthday, String income, String height, String location, String loveactivity,
			String hobbies, String adepthings, String gender, String realname, String yx_reg,String signature,String password,String paypassword) {
		UserInfo info = dataDao.getObjectById(UserInfo.class, CommonUtils.parseLong(user_id, 0));
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		if (!CommonUtils.isEmptyString(mobile)) {
			info.setMobile(mobile);
		}
		if (!CommonUtils.isEmptyString(username)) {
			info.setUser_name(username);
		}
		if (!CommonUtils.isEmptyString(portrait)) {
			info.setPortrait(portrait);
		}
		if (CommonUtils.parseInt(level, 0)!=0) {
			info.setLevel(CommonUtils.parseInt(level, 0));
		}
		if (!CommonUtils.isEmptyString(idcard)) {
			info.setIdcard(idcard);
		}
		if (!CommonUtils.isEmptyString(birthday)) {
			info.setBirthday(birthday);
			String[] string = birthday.split("-");
			int age = year - CommonUtils.parseInt(string[0], 0);
			info.setAge(age);
		}
		if (!CommonUtils.isEmptyString(income)) {
			info.setIncome(CommonUtils.parseInt(income, 0));
		}
		if (CommonUtils.parseInt(height, 0)!=0) {
			info.setHeight(CommonUtils.parseInt(height, 0));
		}
		if (!CommonUtils.isEmptyString(location)) {
			info.setLocation(location);
		}
		if (!CommonUtils.isEmptyString(loveactivity)) {
			info.setLove_activity(loveactivity);
		}
		if (!CommonUtils.isEmptyString(hobbies)) {
			info.setHobbies(hobbies);
		}
		if (!CommonUtils.isEmptyString(adepthings)) {
			info.setAdepthings(adepthings);
		}
		if (!CommonUtils.isEmptyString(gender)) {
			info.setGender(gender);
		}
		if (!CommonUtils.isEmptyString(realname)) {
			info.setReal_name(realname);
		}
		if (!CommonUtils.isEmptyString(yx_reg)) {
			info.setYx_reg(yx_reg);
		}
		if (!CommonUtils.isEmptyString(signature)) {
			info.setSignature(signature);
		}
		if (!CommonUtils.isEmptyString(password)) {
			info.setPassword(password);
		}
		if (!CommonUtils.isEmptyString(paypassword)) {
			info.setPaypassword(paypassword);
		}
		dataDao.updateObject(info);
		return info;

	}

	/**
	 * 验证码一分钟只发送一次
	 * 
	 * @param phone
	 * @return
	 */
	public long getPhoneOneMu(String phone) {
		String hql = "select count(id) from VcodeRecord where phone = :phone and timestampdiff(minute ,time,now()) < 1";
		long count = (long) dataDao.getFirstObjectViaParam(hql, new String[] { "phone" }, phone);
		return count;
	}

	/**
	 * 修改匿名用户信息
	 * 
	 * @param id
	 * @param nickname
	 * @param portrait
	 * @param intro
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateUserAnonymous(String id, String nickname, String portrait, String intro) {
		UserAnonymous info = dataDao.getObjectById(UserAnonymous.class, CommonUtils.parseLong(id, 0));
		if (!CommonUtils.isEmptyString(nickname)) {
			info.setNick_name(nickname);
		}
		if (!CommonUtils.isEmptyString(portrait)) {
			info.setPortrait(portrait);
		}
		if (!CommonUtils.isEmptyString(intro)) {
			info.setIntro(intro);
		}
		dataDao.updateObject(info);
	}

	/**
	 * 查询守护神信息
	 * 
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<UserInfo> userGuard(long id, int page) {
		String hql = " from UserFavorability where user_id = :id or relation_id = :id order by favorability desc";
		List<UserFavorability> info = (List<UserFavorability>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page,
				new String[] { "id" }, id);
		List<UserInfo> userinfos = new ArrayList<>();
		if (info != null && info.size() > 0) {
			for (UserFavorability userFavorability : info) {
				UserInfo userinfo = null;
				if (userFavorability.getUser_id() == id) {
					userinfo = dataDao.getObjectById(UserInfo.class, userFavorability.getRelation_id());
				} else {
					userinfo = dataDao.getObjectById(UserInfo.class, userFavorability.getUser_id());
				}
				if (userinfo != null) {
					userinfos.add(userinfo);
				}
			}
		}
		return userinfos;
	}

	/**
	 * 忘记密码
	 * 
	 * @param mobile
	 * @param password
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public UserInfo changePassUser(String mobile, String password) {
		String hql = " from UserInfo where mobile = :mobile";
		UserInfo info = (UserInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "mobile" }, mobile);
		if (info != null) {
			info.setPassword(password);
			dataDao.updateObject(info);
			return info;
		} else {
			return info;
		}
	}

	/**
	 * 搜索范围内用户信息
	 * 
	 * @param keywords
	 * @param type
	 * @param longtitude
	 * @param latitude
	 * @param page
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@SuppressWarnings("unchecked")
	public List<UserInfo> UserList(String keywords, int type, String longitude1, String latitude1, String page1,
			String user_id) {
		double longitude = CommonUtils.parseDouble(latitude1, 0);
		double latitude = CommonUtils.parseDouble(latitude1, 0);
		long uid = CommonUtils.parseLong(user_id, 0);
		int page = CommonUtils.parseInt(page1, 0);
		List<UserInfo> info = new ArrayList<UserInfo>();
		String hql = "from UserInfo where id != " + user_id + " ";
		if (!CommonUtils.isEmptyString(keywords)) {
			hql += " and user_name like '%" + keywords + "%'";
		}
		if (type == 0) {
			hql += " order by create_time desc";
			info = (List<UserInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page, null);
		} else if (type == 1) {

			UserLocation location = (UserLocation) dataDao.getFirstObjectViaParam(
					" from UserLocation where user_id = :user_id", new String[] { "user_id" }, uid);

			// 如果 经纬度没有

			if (location != null) {
				location.setLatitude(latitude);
				location.setLongitude(longitude);

				dataDao.updateObject(location);
			}

			String hql2 = " select user_id from UserLocation where user_id != " + user_id + " order by getdistance("
					+ longitude + "," + latitude + ",longitude, latitude) asc";
			List<Long> list = (List<Long>) dataDao.pageQueryViaParam(hql2, DsConstant.PAGESIZE, page, null);
			if (list != null && list.size() > 0) {
				for (Long id : list) {
					UserInfo info2 = dataDao.getObjectById(UserInfo.class, id);
					if (info2 != null) {
						info.add(info2);
					}
				}
			}
		} else if (type == 2) {
			if (!CommonUtils.isEmptyString(longitude1) && !CommonUtils.isEmptyString(latitude1)) {
				UserLocation location = (UserLocation) dataDao.getFirstObjectViaParam(
						" from UserLocation where user_id = :user_id", new String[] { "user_id" }, uid);

				// 如果 经纬度没有

				if (location != null) {
					location.setLatitude(latitude);
					location.setLongitude(longitude);

					dataDao.updateObject(location);
				}

				String hql2 = " select l.user_id from UserLocation as l , UserInfo as u where l.user_id != " + user_id
						+ " and u.gender = '男' and l.user_id = u.id order by getdistance(" + longitude + "," + latitude
						+ ",longitude, latitude) asc";
				List<Long> list = (List<Long>) dataDao.pageQueryViaParam(hql2, DsConstant.PAGESIZE, page, null);
				if (list != null && list.size() > 0) {
					for (Long id : list) {
						UserInfo info2 = dataDao.getObjectById(UserInfo.class, id);
						if (info2 != null) {
							info.add(info2);
						}
					}
				}
			} else {
				hql += " and gender = '男'";
				info = (List<UserInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page, null);
			}
		} else if (type == 3) {
			if (!CommonUtils.isEmptyString(longitude1) && !CommonUtils.isEmptyString(latitude1)) {
				UserLocation location = (UserLocation) dataDao.getFirstObjectViaParam(
						" from UserLocation where user_id = :user_id", new String[] { "user_id" }, uid);

				// 如果 经纬度没有

				if (location != null) {
					location.setLatitude(latitude);
					location.setLongitude(longitude);

					dataDao.updateObject(location);
				}

				String hql2 = " select l.user_id from UserLocation as l , UserInfo as u where l.user_id != " + user_id
						+ " and u.gender = '女' and l.user_id = u.id order by getdistance(" + longitude + "," + latitude
						+ ",longitude, latitude) asc";
				List<Long> list = (List<Long>) dataDao.pageQueryViaParam(hql2, DsConstant.PAGESIZE, page, null);
				if (list != null && list.size() > 0) {
					for (Long id : list) {
						UserInfo info2 = dataDao.getObjectById(UserInfo.class, id);
						if (info2 != null) {
							info.add(info2);
						}
					}
				}
			} else {
				hql += " and gender = '女'";
				info = (List<UserInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page, null);
			}
		}

		if (info != null && info.size() > 0) {
			String atthql = " select relation from UserAttention where from_id = :from_id and to_id = :to_id ";
			String[] attparams = new String[] { "from_id", "to_id" };
			for (UserInfo userInfo : info) {
				// 通过自己的id 和 找到的用户的id去查询关系表
				Integer attent = (Integer) dataDao.getFirstObjectViaParam(atthql, attparams, uid, userInfo.getId());
				if (attent != null) {
					userInfo.setRelation(attent);
				} else {
					userInfo.setRelation(0);
				}
			}
		}

		return info;
	}

	/**
	 * 返回范围内匿名用户信息
	 * 
	 * @param page
	 * @param longtitude
	 * @param latitude
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<UserAnonymous> userAnonymous(String page, String longitude, String latitude, String userid) {
		List<UserAnonymous> list = new ArrayList<>();
		if (longitude != null && latitude != null) {
			UserLocation location = (UserLocation) dataDao.getFirstObjectViaParam(
					" from UserLocation where user_id = :userid", new String[] { "userid" },
					CommonUtils.parseLong(userid, 0));
			if (location != null) {
				location.setLatitude(CommonUtils.parseDouble(latitude, 0));
				location.setLongitude(CommonUtils.parseDouble(longitude, 0));

				dataDao.updateObject(location);
			} else {
				UserLocation loca = new UserLocation();
				loca.setLatitude(CommonUtils.parseDouble(latitude, 0));
				loca.setLongitude(CommonUtils.parseDouble(longitude, 0));
				loca.setUser_id(CommonUtils.parseLong(userid, 0));
			}
			String hql = " select u.user_id from UserLocation u right join UserAnonymous a on a.user_id = u.user_id where u.user_id != "
					+ userid + " order by getdistance(" + CommonUtils.parseDouble(longitude, 0) + ","
					+ CommonUtils.parseDouble(latitude, 0) + ", u.longitude, u.latitude) asc";
			List<Long> list2 = (List<Long>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE,
					CommonUtils.parseInt(page, 0), null);
			if (list != null && list2.size() > 0) {
				for (Long user_id : list2) {
					String hql2 = " from UserAnonymous where user_id = :user_id";
					UserAnonymous info = (UserAnonymous) dataDao.getFirstObjectViaParam(hql2,
							new String[] { "user_id" }, user_id);
					if (info != null) {
						list.add(info);
					}

				}
				return list;
			}
		}
		return list;
	}

	/**
	 * 
	 * @Description 通过 user_id 查找用户信息
	 * @param user_id
	 * @return
	 */
	public UserInfo getById(long user_id) {
		UserInfo user = null;
		user = dataDao.getObjectById(UserInfo.class, user_id);
		return user;
	}

	/**
	 * 
	 * @Description 修改用户信息
	 * @param user
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateUserInfo(UserInfo user) {
		dataDao.updateObject(user);
	}

	/**
	 * 
	 * @Description 新增用户信息
	 * @param user
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addUserInfo(UserInfo user) {
		dataDao.addObject(user);
	}

	/**
	 * 
	 * @Description 通过电话号码查找用户信息
	 * @param phone
	 * @return
	 */
	public UserInfo getUserByPhone(String mobile) {
		String hql = " from UserInfo where mobile = :mobile";
		UserInfo user = null;
		user = (UserInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "mobile" }, mobile);
		return user;
	}

	/**
	 * 
	 * @Description 获取不是查询用户的用户信息
	 * @param mobile
	 * @param id
	 * @return
	 */
	public UserInfo getMobile(String mobile, long id) {
		String hql = " from UserInfo where mobile = :mobile and id != :id";
		UserInfo info = (UserInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "mobile", "id" }, mobile, id);
		return info;
	}

	/**
	 * 
	 * @Description 通过条件查找用户信息
	 * @param phone
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageBodyModel<UserInfo> getByParams(String mobile, String nick_name, long user_id, Integer pageIndex, Integer pageSize) {

		String wherehql = " where 1 = 1 ";
		if (!CommonUtils.isEmptyString(mobile)) {
			wherehql += " and mobile = '" + mobile + "'";
		}
		if (!CommonUtils.isEmptyString(nick_name)) {
			wherehql += " and user_name = '" + nick_name + "'";
		}
		if (user_id != 0) {
			wherehql += " and id = " + user_id + " ";
		}
		String orderhql = " order by id desc";
		String chql = "select count(u) from UserInfo u " + wherehql;
		String hql = " from UserInfo " + wherehql + orderhql;

		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<UserInfo> result = new PageBodyModel<UserInfo>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, pageSize);
			List<UserInfo> users = new ArrayList<UserInfo>();
			users = (List<UserInfo>) dataDao.pageQueryViaParam(hql, pageSize, page.getPageIndex(), null);
			if (users != null && users.size() > 0) {
				for (UserInfo userInfo : users) {
					if (!CommonUtils.isEmptyString(userInfo.getPortrait())) {
						userInfo.setPortrait(CommonUtils.getFileRootUrl() + "cache/" + userInfo.getPortrait());
					}
				}
			}
			result = new PageBodyModel<UserInfo>(page, users);
		}
		return result;
	}

	/**
	 * 
	 * @Description 获取全部的兴趣爱好
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<HobbyInfo> getHobbyList() {
		String hql = " from HobbyInfo order by create_time desc";
		List<HobbyInfo> list = (List<HobbyInfo>) dataDao.getObjectsViaParam(hql, null);
		return list;
	}

	/**
	 * 
	 * @Description 获取兴趣爱好
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageBodyModel<HobbyInfo> getHobbyInfo(Integer pageIndex, Integer pageSize) {
		String chql = "select count(u) from HobbyInfo u ";
		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<HobbyInfo> result = new PageBodyModel<HobbyInfo>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, pageSize);
			List<HobbyInfo> users = new ArrayList<HobbyInfo>();
			String hql = " from HobbyInfo ";
			users = (List<HobbyInfo>) dataDao.pageQueryViaParam(hql, pageSize, page.getPageIndex(), null);
			result = new PageBodyModel<HobbyInfo>(page, users);
		}
		return result;
	}

	/**
	 * 
	 * @Description 通过 id 查找兴趣爱好
	 * @param id
	 * @return
	 */
	public HobbyInfo getHobbyById(long id) {
		HobbyInfo info = null;
		info = dataDao.getObjectById(HobbyInfo.class, id);
		return info;
	}

	/**
	 * 
	 * @Description 修改兴趣爱好
	 * @param info
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateHobby(HobbyInfo info) {
		dataDao.updateObject(info);
	}

	/**
	 * 
	 * @Description 新增兴趣爱好
	 * @param info
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addHobby(HobbyInfo info) {
		dataDao.addObject(info);
	}

	/**
	 * 
	 * @Description 通过 id 删除兴趣爱好
	 * @param id
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteHobbyById(long id) {
		HobbyInfo info = null;
		info = dataDao.getObjectById(HobbyInfo.class, id);
		if (info != null) {
			dataDao.deleteObject(info);
		}
	}

	/**
	 * 
	 * @Description 通过 hobby_name 获取非该 id 下的兴趣爱好
	 * @param hobby_name
	 * @param id
	 * @return
	 */
	public HobbyInfo getHobbyByNameid(String hobby_name, long id) {
		String hql = " from HobbyInfo where hobby_name = :hobby_name and id != :id";
		HobbyInfo info = null;
		info = (HobbyInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "hobby_name", "id" }, hobby_name, id);
		return info;
	}

	/**
	 * 
	 * @Description 通过 hobby_name 获取兴趣爱好信息
	 * @param hobby_name
	 * @return
	 */
	public HobbyInfo getHobbyByName(String hobby_name) {
		String hql = " from HobbyInfo where hobby_name = :hobby_name";
		HobbyInfo info = null;
		info = (HobbyInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "hobby_name" }, hobby_name);
		return info;
	}

	/**
	 * 
	 * @Description 通过 user_id 和卡号查找用户账号信息
	 * @param user_id
	 * @param card_num
	 * @return
	 */
	public UserAccount getUserAccountByUseridCard(long user_id, String card_num) {
		String hql = " from UserAccount where user_id = :user_id and card_num = :card_num";
		UserAccount account = (UserAccount) dataDao.getFirstObjectViaParam(hql, new String[] { "user_id", "card_num" },
				user_id, card_num);
		return account;
	}

	/**
	 * 
	 * @Description 增加经验值：送礼物时 exp=0, 直接添加exp时 gift_id=0
	 * @param user_id
	 * @param gift_id
	 * @param exp
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addExp(UserInfo user, long gift_id, int exp) {
		if (gift_id != 0) { // 通过送礼物增加经验值
			GiftInfo gift = dataDao.getObjectById(GiftInfo.class, gift_id);
			if (gift != null) {
				exp = gift.getPoint();
			}
		}
		user.setExp(user.getExp() + exp);

		// 通过用户的等级，获取等级表中的信息
		int lv = 1;
		if (user.getLevel() != 0) {
			lv = user.getLevel();
		}
		LvInfo info = (LvInfo) dataDao.getFirstObjectViaParam(" from LvInfo where lv = :lv", new String[] { "lv" }, lv);
		if (info != null) {
			if (user.getExp() >= (info.getExp() + info.getTotal())) {
				user.setLevel(lv + 1);
			}
		}

		dataDao.updateObject(user);
	}

	/**
	 * 
	 * @Description 获取用户详情的守护神头像
	 * @param user_id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> getIndexUser(long user_id) {
		String hql = " from UserFavorability where user_id = :user_id or relation_id = :user_id order by favorability desc";
		List<UserFavorability> info = (List<UserFavorability>) dataDao.pageQueryViaParam(hql, 3, 1,
				new String[] { "user_id" }, user_id);
		List<String> userinfos = new ArrayList<>();
		String hql1 = " select portrait from UserInfo where id = :id";
		String[] paramstrs = new String[] { "id" };
		if (info != null && info.size() > 0) {
			for (UserFavorability userFavorability : info) {
				String portrait = "";
				if (userFavorability.getUser_id() == user_id) {
					portrait = (String) dataDao.getFirstObjectViaParam(hql1, paramstrs,
							userFavorability.getRelation_id());
				} else {
					portrait = (String) dataDao.getFirstObjectViaParam(hql1, paramstrs, userFavorability.getUser_id());
				}
				if (CommonUtils.isEmptyString(portrait)) {
					userinfos.add("");
				} else {
					userinfos.add(portrait);
				}
			}
		}
		return userinfos;
	}

	/**
	 * 
	 * @Description 获取搭讪列表
	 * @param keywords
	 * @param longitude
	 * @param latitude
	 * @param page
	 * @param user_id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<UserInfo> getUserAccost(String keywords, double longitude, double latitude, int page, long user_id) {
		UserLocation location = (UserLocation) dataDao.getFirstObjectViaParam(
				" from UserLocation where user_id = :user_id", new String[] { "user_id" }, user_id);

		// 如果 经纬度没有

		if (location != null) {
			location.setLatitude(latitude);
			location.setLongitude(longitude);

			dataDao.updateObject(location);
		}
		String hql = " from UserInfo where gender = '男'";
		if (!CommonUtils.isEmptyString(keywords)) {
			hql += " and user_name like '%" + keywords + "%' ";
		}
		hql += " order by create_time desc ";
		List<UserInfo> users = new ArrayList<>();
		users = (List<UserInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page, null);
		String chql = " from MomentInfo where user_id = :user_id order by create_time desc";
		String[] params = new String[] { "user_id" };
		if (users != null && users.size() > 0) {
			for (UserInfo user : users) {
				MomentInfo moment = (MomentInfo) dataDao.getFirstObjectViaParam(chql, params, user.getId());
				if (moment != null) {
					moment.setUser(null);
					user.setMoment(moment);
				}
			}

		}

		return users;
	}

	/**
	 * 
	 * @Description 通过 id 获取用户信息，并查询两人的关系
	 * @param from_id
	 * @param to_id
	 * @return
	 */
	public UserInfo getByTwoid(long from_id, long to_id) {
		UserInfo info = dataDao.getObjectById(UserInfo.class, to_id);
		if (info != null) {
			String hql = "select relation from UserAttention where from_id = :from_id and to_id = :to_id";
			Integer attent = (Integer) dataDao.getFirstObjectViaParam(hql, new String[] { "from_id", "to_id" }, from_id,
					to_id);
			if (attent != null) {
				info.setRelation(attent);
			} else {
				info.setRelation(0);
			}
		}
		return info;
	}

	/**
	 * 
	 * @Description 获得所有的等级
	 * @return
	 */
	public List<LvInfo> getLvinfo() {
		List<LvInfo> info = dataDao.getAllObject(LvInfo.class);
		return info;
	}

	/**
	 * 
	 * @Description 分页获得等级
	 * @param pageIndex
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageBodyModel<LvInfo> pageLvs(Integer pageIndex) {
		String chql = " select count(l) from LvInfo l";
		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<LvInfo> result = new PageBodyModel<>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, DsConstant.PAGESIZE);
			String hql = " from LvInfo order by lv ";
			List<LvInfo> infos = new ArrayList<>();
			infos = (List<LvInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page.getPageIndex(), null);
			result = new PageBodyModel<>(page, infos);
		}

		return result;
	}

	/**
	 * 
	 * @Description 通过等级获得等级信息
	 * @param lv
	 * @return
	 */
	public LvInfo getLvInfoByLv(int lv) {
		String hql = " from LvInfo where lv = :lv";
		LvInfo info = (LvInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "lv" }, lv);
		return info;
	}

	/**
	 * 
	 * @Description 通过 id 获得等级信息
	 * @param id
	 * @return
	 */
	public LvInfo getLvinfoById(long id) {
		LvInfo info = dataDao.getObjectById(LvInfo.class, id);
		return info;
	}

	/**
	 * 
	 * @Description 修改等级信息
	 * @param info
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void updateLvinfo(LvInfo info) {
		dataDao.updateObject(info);
	}

	/**
	 * 
	 * @Description 新增等级信息
	 * @param info
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void addlvinfo(LvInfo info) {
		dataDao.addObject(info);
	}

	/**
	 * 
	 * @Description 获取不是查询 id 下的等级信息
	 * @param id
	 * @param lv
	 * @return
	 */
	public LvInfo getLvInfoByIdLv(long id, int lv) {
		String hql = " from LvInfo where lv = :lv and id != :id";
		LvInfo info = (LvInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "lv", "id" }, lv, id);
		return info;
	}

	/**
	 * 
	 * @Description 获得最后一个等级信息
	 * @return
	 */
	public LvInfo getLastLv() {
		String hql = " from LvInfo order by lv desc";
		LvInfo info = (LvInfo) dataDao.getFirstObjectViaParam(hql, null);
		return info;
	}

	/**
	 * 
	 * @Description 获得该等级之后的所有等级
	 * @param lv
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<LvInfo> getFirstLv(int lv) {
		String hql = " from LvInfo where lv >= :lv ";
		List<LvInfo> infos = (List<LvInfo>) dataDao.getObjectsViaParam(hql, new String[] { "lv" }, lv);
		return infos;
	}

	/**
	 * 
	 * @Description 删除等级
	 * @param id
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deletelvinfo(long id) {
		LvInfo info = null;
		info = dataDao.getObjectById(LvInfo.class, id);
		if (info != null) {
			dataDao.deleteObject(info);
		}
	}

	/**
	 * 
	 * @Description 通过条件获取用户列表
	 * @param keywords
	 * @param type
	 * @param longitude
	 * @param latitude
	 * @param page
	 * @param user_id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<UserInfo> getUserList(String keywords, int type, double longitude, double latitude, int page,
			long user_id, int sex) {
		List<UserInfo> info = new ArrayList<UserInfo>();

		if (type == 0) {
			String hql = " from UserInfo where id != " + user_id + " ";
			if (!CommonUtils.isEmptyString(keywords)) {
				hql += " and user_name like '%" + keywords + "%'";
			}

			if (sex == 1) {
				hql += " and gender = '男' ";
			} else if (sex == 2) {
				hql += " and gender = '女' ";
			}

			hql += " order by create_time desc";
			info = (List<UserInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page, null);
		} if(type == 1) {
			UserLocation location = (UserLocation) dataDao.getFirstObjectViaParam(
					" from UserLocation where user_id = :user_id", new String[] { "user_id" }, user_id);
			if (location != null) {
				location.setLatitude(latitude);
				location.setLongitude(longitude);

				dataDao.updateObject(location);
			} else {
				UserLocation loca = new UserLocation();
				loca.setLongitude(longitude);
				loca.setLatitude(latitude);
				loca.setUser_id(user_id);

				dataDao.addObject(loca);
			}

			String hql = " select l.user_id from UserLocation l left join UserInfo u on l.user_id = u.id"
					+ " where l.user_id != " + user_id + " ";
			if (!CommonUtils.isEmptyString(keywords)) {
				hql += " and u.user_name like '%" + keywords + "%'";
			}

			if (sex == 1) {
				hql += " and u.gender = '男' ";
			} else if (sex == 2) {
				hql += " and u.gender = '女' ";
			}
			hql += " order by getdistance(" + longitude + "," + latitude + ", l.longitude, l.latitude)";

			List<Long> list = (List<Long>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page, null);
			if (list != null && list.size() > 0) {
				for (Long id : list) {
					UserInfo info2 = dataDao.getObjectById(UserInfo.class, id);
					if (info2 != null) {
						info.add(info2);
					}
				}
			}
		}
		if (type == 2) {
			String hql = " from UserInfo where id != " + user_id + " and is_home =1";
			if (sex == 1) {
				hql += " and gender = '男' ";
			} else if (sex == 2) {
				hql += " and gender = '女' ";
			}
			hql += " order by top_time desc";
			info = (List<UserInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page, null);
		}
		
		if (type == 3) {
			String hql = " from UserInfo where id != " + user_id + " ";
			if (sex == 1) {
				hql += " and gender = '男' ";
			} else if (sex == 2) {
				hql += " and gender = '女' ";
			}
			hql += " order by fans_num desc";
			info = (List<UserInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page, null);
		}

		if (info != null && info.size() > 0) {
			String atthql = " select relation from UserAttention where from_id = :from_id and to_id = :to_id ";
			String[] attparams = new String[] { "from_id", "to_id" };
			for (UserInfo userInfo : info) {
				// 通过自己的id 和 找到的用户的id去查询关系表
				Integer attent = (Integer) dataDao.getFirstObjectViaParam(atthql, attparams, user_id, userInfo.getId());
				if (attent != null) {
					userInfo.setRelation(attent);
				} else {
					userInfo.setRelation(0);
				}
			}
		}

		return info;
	}

	/**
	 * 
	 * @Description 通过 user_id 获取匿名用户信息
	 * @param user_id
	 * @return
	 */
	public UserAnonymous getAnonymousByUserid(long user_id) {
		String hql = " from UserAnonymous where user_id = :user_id ";
		UserAnonymous anon = (UserAnonymous) dataDao.getFirstObjectViaParam(hql, new String[] { "user_id" }, user_id);
		return anon;
	}

	/**
	 * 
	 * @Description 通过等级删除等级
	 * @param lv
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteLvinfoByLv(int lv) {
		String hql = " from LvInfo where lv = :lv ";
		LvInfo info = (LvInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "lv" }, lv);
		if (info != null) {
			dataDao.deleteObject(info);
		}
	}

	/**
	 * 
	 * @Description 通过等级批量删除等级
	 * @param lvs
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteLvinfoByLvs(String lvs) {
		String hql = " delete from t_lv_info where lv in (" + lvs + ")";
		dataDao.deleteBySql(hql);
	}

	/**
	 * 
	 * @Description 通过环信id 获取匿名用户信息
	 * @param anonymous_id
	 * @return
	 */
	public UserAnonymous getAnonymousByAnonid(String anonymous_id) {
		String hql = " from UserAnonymous where anonymous_id = :anonymous_id ";
		UserAnonymous anon = (UserAnonymous) dataDao.getFirstObjectViaParam(hql, new String[] { "anonymous_id" },
				anonymous_id);
		return anon;
	}

	/**
	 * 存视频评级
	 * @param evaluate
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void saveVideoEvaluate(VideoEvaluate evaluate) {
		dataDao.addObject(evaluate);
		
	}

	/**
	 * 保存反馈
	 * @param feed
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void saveFeedBack(FeedBack feed) {
		dataDao.addObject(feed);
	}

	/**
	 * 获得帮助信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<HelpInfo> getHelpInfo() {
		String hql = "from HelpInfo where 1=1";
		List<HelpInfo> list = (List<HelpInfo>) dataDao.getObjectsViaParam(hql, null);
		return list;
	}
	/**
	 * 获得视频评价平均值
	 * @param rateid
	 * @return
	 */
	public int getAVG(long rateid) {
		String hql = "select avg(grade) from VideoEvaluate where rate_id = :rate_id";
		double avg =  (double) dataDao.getFirstObjectViaParam(hql, new String[] { "rate_id" },rateid);
		int avgs = (int) Math.round(avg);
		return avgs;
		
		
	}

	@SuppressWarnings("unchecked")
	public PageBodyModel<ReportInfo> pageReport(Integer pageIndex) {
		String chql = " select count(id) from ReportInfo";
		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<ReportInfo> result = new PageBodyModel<>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, DsConstant.PAGESIZE);
			String hql = " from ReportInfo order by addtime desc";
			List<ReportInfo> infos = new ArrayList<>();
			infos = (List<ReportInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page.getPageIndex(), null);
			for (ReportInfo reportInfo : infos) {
				UserInfo user = dataDao.getObjectById(UserInfo.class, reportInfo.getUser_id());
				UserInfo to_user = dataDao.getObjectById(UserInfo.class, reportInfo.getMine_id());
				if (user!=null) {
					reportInfo.setUser(user);
					reportInfo.setTo_user(to_user);
				}
			}
			result = new PageBodyModel<>(page, infos);
		}

		return result;
	}

	/**
	 * 获得视频评价列表
	 * @param userid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<VideoEvaluate> getVideoEvaluatelist(long userid,int page) {
		String hql = "from VideoEvaluate where rate_id = :rate_id order by create_time desc";
		List<VideoEvaluate> list = (List<VideoEvaluate>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page, new String[] { "rate_id" },userid);
		if (list != null) {
			for (VideoEvaluate videoEvaluate : list) {
				UserInfo user = dataDao.getObjectById(UserInfo.class, videoEvaluate.getUser_id());
				videoEvaluate.setUser(user);
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public PageBodyModel<UserInfo> Homeuser(Integer pageIndex) {
		String chql = " select count(id) from UserInfo where is_home = 1 order by top_time desc";
		long count = (long) dataDao.getFirstObjectViaParam(chql, null);
		PageBodyModel<UserInfo> result = new PageBodyModel<>();
		if (count > 0) {
			PageModel page = PageModel.createPage(count, pageIndex, DsConstant.PAGESIZE);
			String hql = " from UserInfo where is_home = 1 order by top_time desc";
			List<UserInfo> infos = new ArrayList<>();
			infos = (List<UserInfo>) dataDao.pageQueryViaParam(hql, DsConstant.PAGESIZE, page.getPageIndex(), null);
			result = new PageBodyModel<>(page, infos);
		}

		return result;
	}

	public long getfansnum(long tid) {
	String hql = "select count(id) from UserAttention where to_id = :to_id";
	long count = (long) dataDao.getFirstObjectViaParam(hql, new String[] { "to_id" }, tid);
	return count;	
	}

	public UserAccount getUserAccountByUserid(long id) {
		String hql = "from UserAccount where user_id = :user_id";
		UserAccount account =  (UserAccount) dataDao.getFirstObjectViaParam(hql, new String[]{"user_id"}, id);
		return account;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public UserInfo changePayPassword(String mobile, String paypassword) {
		String hql = " from UserInfo where mobile = :mobile";
		UserInfo info = (UserInfo) dataDao.getFirstObjectViaParam(hql, new String[] { "mobile" }, mobile);
		if (info != null) {
			info.setPaypassword(paypassword);;
			dataDao.updateObject(info);
			return info;
		} else {
			return info;
		}
	}
}